#!/bin/bash
set -ex

stable=0.9
for V in master $stable; do
  docker push $DOCKER_REPO:$V
done

function get_hub_version() {
  rm -f hub_version
  V=$1
  docker run --rm -v $PWD:/version -u $(id -u) -i $DOCKER_REPO:$V sh -c 'jupyterhub --version > /version/hub_version'
  hub_xyz=$(cat hub_version)
  split=( ${hub_xyz//./ } )
  hub_xy="${split[0]}.${split[1]}"
  # add .dev on hub_xy so it's 1.0.dev
  if [[ ! -z "${split[3]:-}" ]]; then
    hub_xy="${hub_xy}.${split[3]}"
  fi
}
# tag e.g. 0.8.1 with 0.8
get_hub_version $stable
docker tag $DOCKER_REPO:$stable $DOCKER_REPO:$hub_xyz
docker push $DOCKER_REPO:$hub_xyz

# tag e.g. 0.9 with master
get_hub_version master
docker tag $DOCKER_REPO:master $DOCKER_REPO:$hub_xy
docker push $DOCKER_REPO:$hub_xy
docker tag $DOCKER_REPO:master $DOCKER_REPO:$hub_xyz
docker push $DOCKER_REPO:$hub_xyz
